package org.skyfree.ghyll.tcard.action;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.skyfree.ghyll.tcard.control.InputCardNameDialog;
import org.skyfree.ghyll.tcard.core.ITCard;

public class AddMenuItemAction extends AbstractMenuItemAction {

	public AddMenuItemAction(int style, String text, int Accelerator,
			Image image,TreeViewer treeViewer) 
	{
		super(style, text, Accelerator, image,treeViewer);
	}

	@Override
	public void execute(Object source) throws Exception 
	{
		ITCard card = (ITCard) this.item.getData();
		InputCardNameDialog inputName = new InputCardNameDialog(this.treeViewer.getTree().getShell(),card.getWorkpiece());
		if(inputName.open()==IDialogConstants.OK_ID)
		{
			ITCard newCard = card.getWorkpiece().createTCard(card, inputName.getCardName(),inputName.isComp(),true);
			newCard.setItemID(inputName.getCardID());
			newCard.save();
			if(newCard!=null)
			{
				treeViewer.refresh();
			}
		}
	}

	@Override
	public boolean isEnable() 
	{
		ITCard card = (ITCard) this.item.getData();
		return card.isCompositeTCard();
	}
	
}
